
package com.joolun.cloud.pay.api.controller.ali;

import com.alipay.easysdk.factory.MultipleFactory;
import com.alipay.easysdk.payment.app.models.AlipayTradeAppPayResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeRefundResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.alipay.easysdk.payment.wap.models.AlipayTradeWapPayResponse;
import com.joolun.cloud.common.core.util.R;
import com.joolun.cloud.common.security.annotation.Inside;
import com.joolun.cloud.pay.api.config.AliPayConfiguration;
import com.joolun.cloud.pay.common.constant.PayConstants;
import com.joolun.cloud.pay.common.dto.AliBaseRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

/**
 * 支付宝支付
 *
 * @author www.joolun.com
 * @date 2021-03-23 21:26:35
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/alipay")
@Api(value = "alipay", tags = "支付宝支付")
public class AliPayController {

	/**
	 * 调用统一下单接口（支付宝）
	 *
	 * @param aliBaseRequest 统一下单请求参数
	 * @return
	 */
	@ApiOperation(value = "调用统一下单接口（支付宝）")
	@Inside
	@PostMapping("/unifiedOrder")
	public R unifiedOrder(@RequestBody AliBaseRequest aliBaseRequest) throws Exception {
		log.debug("调用统一下单接口（支付宝）");
		Map<String,Object> params = aliBaseRequest.getParams();
		String tradeType = params.get("tradeType").toString();
		MultipleFactory multipleFactory = AliPayConfiguration.getFactory();
		if("NATIVE".equals(tradeType)){//电脑网页
			AlipayTradePagePayResponse alipayTradePagePayResponse = multipleFactory.Page()
					.agent(aliBaseRequest.getAppAuthToken())
					.asyncNotify(params.get("notifyUrl").toString())
					.pay(params.get("subject").toString(),params.get("outTradeNo").toString(),params.get("totalAmount").toString(),params.get("returnUrl").toString());
			return R.ok(alipayTradePagePayResponse);
		}else if("APP".equals(tradeType)){//APP
			AlipayTradeAppPayResponse alipayTradeAppPayResponse = multipleFactory.App()
					.agent(aliBaseRequest.getAppAuthToken())
					.asyncNotify(params.get("notifyUrl").toString())
					.pay(params.get("subject").toString(),params.get("outTradeNo").toString(),params.get("totalAmount").toString());
			return R.ok(alipayTradeAppPayResponse);
		}else if("MWEB".equals(tradeType)){//手机网站支付
			AlipayTradeWapPayResponse alipayTradeWapPayResponse = multipleFactory.Wap()
					.agent(aliBaseRequest.getAppAuthToken())
					.asyncNotify(params.get("notifyUrl").toString())
					.pay(params.get("subject").toString(),params.get("outTradeNo").toString(),params.get("totalAmount").toString(),params.get("quitUrl").toString(),params.get("returnUrl").toString());
			return R.ok(alipayTradeWapPayResponse);
		}else{
			return R.failed("该订单无支付类型");
		}
	}


	/**
	 * 查询订单（支付宝）
	 * @param aliBaseRequest
	 * @return
	 */
	@ApiOperation(value = "查询订单（支付宝）")
	@Inside
	@PostMapping("/queryOrder")
	public R queryOrder(@RequestBody AliBaseRequest aliBaseRequest) throws Exception {
		log.debug("查询订单（支付宝）");
		Map<String,Object> params = aliBaseRequest.getParams();
		MultipleFactory multipleFactory = AliPayConfiguration.getFactory();
		AlipayTradeQueryResponse alipayTradeQueryResponse = multipleFactory.Common().agent(aliBaseRequest.getAppAuthToken()).query(params.get("outTradeNo").toString());
		if(PayConstants.ALI_RES_CODE_SUCCESS.equals(alipayTradeQueryResponse.getCode())){
			return R.ok(alipayTradeQueryResponse);
		}else{
			return R.failed(alipayTradeQueryResponse,alipayTradeQueryResponse.getMsg()+"："+alipayTradeQueryResponse.getSubMsg());
		}
	}

	/**
	 * 申请退款（支付宝）
	 * @param aliBaseRequest
	 * @return
	 */
	@ApiOperation(value = "申请退款（支付宝）")
	@Inside
	@PostMapping("/refundOrder")
	public R refundOrder(@RequestBody AliBaseRequest aliBaseRequest) throws Exception {
		log.debug("申请退款（支付宝）");
		Map<String,Object> params = aliBaseRequest.getParams();
		MultipleFactory multipleFactory = AliPayConfiguration.getFactory();
		AlipayTradeRefundResponse alipayTradeRefundResponse = multipleFactory.Common().agent(aliBaseRequest.getAppAuthToken())
				.optional("out_request_no",params.get("outRequestNo").toString())
				.refund(params.get("outTradeNo").toString(),params.get("refundAmount").toString());
		if(PayConstants.ALI_RES_CODE_SUCCESS.equals(alipayTradeRefundResponse.getCode())){
			return R.ok(alipayTradeRefundResponse);
		}else{
			return R.failed(alipayTradeRefundResponse,alipayTradeRefundResponse.getMsg()+"："+alipayTradeRefundResponse.getSubMsg());
		}
	}

	@ApiOperation(value = "查询退款（支付宝）")
	@Inside
	@PostMapping("/queryRefundOrder")
	public R queryRefundOrder(@RequestBody AliBaseRequest aliBaseRequest) throws Exception {
		log.debug("查询退款（支付宝）");
		Map<String,Object> params = aliBaseRequest.getParams();
		MultipleFactory multipleFactory = AliPayConfiguration.getFactory();
		AlipayTradeFastpayRefundQueryResponse alipayTradeFastpayRefundQueryResponse = multipleFactory.Common().agent(aliBaseRequest.getAppAuthToken()).queryRefund(params.get("outTradeNo").toString(),params.get("outRequestNo").toString());
		if(PayConstants.ALI_RES_CODE_SUCCESS.equals(alipayTradeFastpayRefundQueryResponse.getCode())){
			return R.ok(alipayTradeFastpayRefundQueryResponse);
		}else{
			return R.failed(alipayTradeFastpayRefundQueryResponse,alipayTradeFastpayRefundQueryResponse.getMsg()+"："+alipayTradeFastpayRefundQueryResponse.getSubMsg());
		}
	}
}
